<?php

class IndexController extends FinalView_Controller_Action
{
    const DATE_FORMAT = 'd-m-Y';
    
    private $_date;
    
    private $_food_intake;
    
    public function init() 
    {
        parent::init();
        
        $this->_date = $this->_getParam('date', date(self::DATE_FORMAT));
        $validator = new Zend_Validate_Date('DD-MM-YYYY');
        if(!$validator->isValid($this->_date)) {
            $this->abort('Invalid date given');
        }
        
        
        
        $this->_food_intake = $this->_getParam('food_intake_code', null);
        if (is_null($this->_food_intake)) {
            $model = new FoodRation;
            if ($last_food_intake = $model->getLastFoodIntake($this->_date)) 
            {
                $query = http_build_query(array(
                    'date' => $this->_date, 
                    'food_intake_code' => $last_food_intake
                ));
                $this->_redirect($this->view->url(array(), 'Statistic') . '?' . $query);
            }
        } else {
            $this->_food_intake = 
                $this->_getParam('food_intake_code', key($this->_getFoodIntakes()));
            $validator = new Zend_Validate_InArray(array_keys($this->_getFoodIntakes()));
            if(!$validator->isValid($this->_food_intake)) {
                $this->abort('Invalid food intake given');
            }
        }
        
        
        
        $this->_statistic();
    }
    
    private function _getFoodIntakes() 
    {
        $model = new FoodIntake;
        return $model->getOptions();
    }
    
    public function indexAction()
    {
        $model = new FoodRation;
        
        if ($this->getRequest()->isPost()) {
            $form = new Default_Form_FoodIntake(
                (count($this->getRequest()->getPost()) - 3) / 2);
            
            if ($form->isValid($this->getRequest()->getPost())) {
                $model->setProducts($form->getValues());
                
                $this->getHelper('FlashMessenger')->addMessage('Сохранено');
                $this->_redirect('?' . http_build_query(
                    array_extract($form->getValues(), array('date', 'food_intake_code'))));
            }
        } else {
            $collection = $model->getProducts($this->_date, $this->_food_intake);
            
            $form = new Default_Form_FoodIntake($collection->count());
            $form->setValues($collection, $this->_date, $this->_food_intake);
        }
        
        $this->view->form = $form;
        $this->view->date = $this->_date;
        $this->view->food_intake = $this->_food_intake;
    }
    
    private function _statistic() 
    {
        $model_food_ration = new FoodRation;
        $model_content = new Content;
        $model_components = new Components;
        
        $food_ration_collection = $model_food_ration->getProducts($this->_date);
        
        $content_collection = $model_content->getContent(
            array_unique($food_ration_collection->toKeyValueArray('id', 'product_id')));
        
        //var_dump($content_collection->toArray(true));
        
        $this->view->food_ration_collection = $food_ration_collection;
        $this->view->components = $model_components->getOptions();
        //$this->view->content_collection = $content_collection;
    }
    
}

